(function(){
    var url = '/index.php?m=Home&c=PointsGoods&a=addPointsOrder';
    //积分
    var score=$(".gift-detail span.rt").html();
    // 购买数量
    var num=Number($(".buyNum").val());
    // 消费积分
    var cost=Number(score)*num;
    // 获取商品的id
    var goods_id = $('#goods_id').val();

    $(".total-counts>p:nth-child(2)>span.rt").html(cost);

    //TODO 当兑换数量改变时，同步更新消耗积分
    $("div.counts img").click(function(){
        num=Number($(".buyNum").val());
        cost=Number(score)*num;
        $(".total-counts>p:nth-child(2)>span.rt").html(cost);
    });
    //todo 确认领取按钮
    $("#btn").click(function(){
        // 获取地址的id
        var address_id = $('#address_id').val();
        var totalScore=Number($(".total-counts>p:nth-child(1)>span.rt").html());
        if(totalScore>=cost){
            //触发ajax
            $.ajax({
                url:url,
                data:{  
                        'goods_count':num,
                        'cost':cost,
                        'address_id':address_id,
                        'goods_id':goods_id
                    },
                dataType:'json',
                type:'post',
                success:function(msg){
                    if (msg.status == 1) {
                        $(".total-counts>p:nth-child(1)>span.rt").html(totalScore-cost);
                        prompt(msg.msg);
                        window.setTimeout(function(){
                            //返回上个界面
                            window.history.go(-2);
                        },1000);
                    }else {
                        prompt(msg.msg);
                    }
                }
            });
        }else{
            prompt("积分不足");
        }
    });

    //todo 点击收货地址，跳转到选择地址页面
    $("body>.top").on("click",".addr",function(){
        $("#receiveAddrTk").show();
        $(".selectAddr").show().css("bottom",0);
    })

})();
